package com.mlog.njyjy.ndop.msg.admin.controller;

import com.mlog.njyjy.mp.starter.maws.response.Resp;
import com.mlog.njyjy.ndop.msg.admin.service.CollectItemService;
import com.mlog.njyjy.ndop.msg.common.support.ItemType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *
 * </p>
 *
 * @author zyz
 * @version v1.0, 2020/8/10
 */
@Api(value = "消息采集项管理")
@RestController
@CrossOrigin
@RequestMapping("/api/collectItems")
public class CollectItemController {

    @Autowired
    private CollectItemService collectItemService;

    @ApiOperation(value = "同步采集项", tags = "消息采集项管理")
    @GetMapping("/{type}/sync")
    public Resp syncItems(@PathVariable ItemType type) {
        boolean success = collectItemService.syncItems(type.name());
        if (success) {
            return Resp.of(collectItemService.listItems(type.name()));
        }
        return Resp.FAIL;
    }

    @ApiOperation(value = "列出采集项", tags = "消息采集项管理")
    @GetMapping("/{type}/list")
    public Resp listItems(@PathVariable ItemType type) {
        return Resp.of(collectItemService.listItems(type.name()));
    }

    @ApiOperation(value = "启用采集项", tags = "消息采集项管理")
    @PostMapping("/{type}/enabled")
    public Resp enabledItems(@PathVariable ItemType type, @RequestParam String ids) {
        boolean success = collectItemService.changeStatus(type.name(), ids, 1);
        return success ? Resp.OK : Resp.fail(500, "启用采集项失败");

    }

    @ApiOperation(value = "取消采集项", tags = "消息采集项管理")
    @PostMapping("/{type}/cancel")
    public Resp disabledItems(@PathVariable ItemType type, @RequestParam String ids) {

        boolean success = collectItemService.changeStatus(type.name(), ids, 0);
        return success ? Resp.OK : Resp.fail(500, "取消采集项失败");
    }

    @ApiOperation(value = "列出启用的采集项", tags = "消息采集项管理")
    @GetMapping("/{type}/enabled")
    public Resp listEnabledItems(@PathVariable ItemType type) {
        return Resp.of(collectItemService.listEnabledItems(type.name()));
    }
}
